<?php

// +----------------------------------------------------------------------
// | Author: pp <124910168@qq.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use controller\BasicAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;

/**
 * 相册管理
 * Class Slider
 * @package app\admin\controller
 * @author  <124910168@qq.com>
 * @date 2017/6/13
 */
class Slider extends BasicAdmin
{
    /**
     * 栏目相册表
     * @var string
     */
    public $brandTable = 'public_brand';
    public $sliderTable = 'public_slider';
    public $sliderItemTable = 'public_slider_item';

    /**
     * 相册管理
     */
    public function index()
    {
        $this->title='相册管理';
        $user = session('user');
        $get = $this->request->get();
        // $Map=[
        //     'create_by'=>$user['id'],
        // ];
        $db = Db::name($this->sliderTable);
        $map = [];
        foreach (['title', 'brand_id', 'status'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '' && $get[$key] != '-1') {
                $map[$key] = ['like', "%{$get[$key]}%"];
            }
        }
        $db->where($map)->order("create_at desc");
        $data = parent::_list($db, true, false);

        $menusp = Db::name($this->brandTable)->where(['review_status' => 1])->select();
        $this->assign('menusp', $menusp);

        return $this->fetch('', $data);
    }

    /**
     * 添加
     */
    public function add()
    {
        $this->user = session('user');
        $this->uid = $this->user['create_by'];
        $menusp = Db::name($this->brandTable)->where(['review_status' => 1])->select();
        $this->assign('menusp', $menusp);
        return $this->_form($this->sliderTable,'form');
    }

    /**
     * 编辑
     */
    public function edit()
    {
        return $this->add();
    }

    /**
     * 删除
     */
    public function del() {
        if (DataService::update($this->sliderTable)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    /**
     * 禁用
     */
    public function forbid() {
        if (DataService::update($this->sliderTable)) {
            $this->success("禁用成功！", '');
        }
        $this->error("禁用失败，请稍候再试！");
    }

    /**
     * 启用
     */
    public function resume() {
        if (DataService::update($this->sliderTable)) {
            $this->success("启用成功！", '');
        }
        $this->error("启用失败，请稍候再试！");
    }


    /**
     * 照片
     */
    public function blist()
    {
        $banner_id = $this->request->param('banner_id');
        $this->assign('banner_id', $banner_id);
        $this->title = '相册管理 - ';
        $get = $this->request->get();
        $map = [];
        if(isset($banner_id)){
            $map['banner_id'] = $banner_id;
        }
        $db = Db::name($this->sliderItemTable)->where($map);

        // 应用搜索条件
        foreach (['title'] as $key) {
            if (isset($get[$key]) && $get[$key] !== '') {
                $db->where($key, 'like', "%{$get[$key]}%");
            }
        }

        // 相册位置名称
        $data = parent::_list($db, true, false);
        // foreach($data['list'] as $key=>$list) {
        //     $data['list'][$key]['banner_name'] = $this->get_banner_name_byid($list['banner_id']);
        // }
        return $this->fetch('', $data);
    }

    /**
     * 添加
     */
    public function additem() {
            $bannerList = Db::name($this->sliderTable)->order('id asc')->select();
            $this->assign('bannerList', $bannerList);
        return $this->_form($this->sliderItemTable, 'itemform');
    }

    /**
     * 编辑
     */
    public function edititem() {
        return $this->add();
    }

    /**
     * 删除
     */
    public function delitem() {
        if (DataService::update($this->sliderItemTable)) {
            $this->success("删除成功！", '');
        }
        $this->error("删除失败，请稍候再试！");
    }

    /**
     * 禁用
     */
    public function forbiditem() {
        if (DataService::update($this->sliderItemTable)) {
            $this->success("禁用成功！", '');
        }
        $this->error("禁用失败，请稍候再试！");
    }

    /**
     * 启用
     */
    public function resumeitem() {
        if (DataService::update($this->sliderItemTable)) {
            $this->success("启用成功！", '');
        }
        $this->error("启用失败，请稍候再试！");
    }
}